import request from '@/utils/request'

export function getTaskSource(query) {
  return new Promise((resolve, reject) => {
    request({
      url: '/get_dataset',
      method: 'get',
      params: {
        project_id: query,
      },
    })
      .then((res) => {
        console.log(res.data)
        resolve({
          code: res.code,
          data: res.data.map((item) => ({
            owner: {
              id: item.user_id,
              name: item.user_name,
              photo: item.avatar_url,
              ip: item.ip,
            },
            data: item.data_set.map((value) => ({
              id: value.dataset_id,
              name: value.dataset_name,
              number: value.dataset_use,
              description: value.dataset_describe,
              path: value.dataset_address,
            })),
          })),
          pagination: res.pagination,
        })
      })
      .catch((error) => {
        reject(error)
      })
  })
}
export function getAttackSource(query) {
  return new Promise((resolve, reject) => {
    request({
      url: '/self_dataset',
      method: 'get',
      params: {
        user_id: query,
      },
    })
      .then((res) => {
        console.log(res)
        resolve({
          code: res.code,
          data: res.data.map((item) => ({
            id: item.dataset_id,
            name: item.dataset_name,
            number: item.size,
            description: item.dataset_describe,
            path: item.dataset_address,
            // auth_number: item.dataset_use,
            // upload_time: item.dataset_time,
            // status: item.dataset_state,
          })),
        })
      })
      .catch((error) => {
        reject(error)
      })
  })
}
// export function getTaskModel(query) {
//   const res = request({
//     url: '/taskboard/getTaskModel',
//     method: 'get',
//     params: query,
//   })
//   // 在这里对结果进行转换
//   // console.log(res)
//   return JSON.parse(res)
// }

export function getTaskModel(query) {
  return request({
    url: '/get_model',
    method: 'get',
    params: {
      project_id: query,
    },
  }).then((res) => {
    console.log(res)
    // 在这里对结果进行转换
    console.log(JSON.parse(res.data.project_model))
    return JSON.parse(res.data.project_model) // 假设返回的数据在res.data中，根据实际情况修改
  })
}
export function ChangeTaskStatus(query) {
  console.log(query)
  return request({
    url: '/change_model',
    method: 'post',
    params: {
      project_id: query.id,
      project_state: query.status,
    },
  })
}
export function saveTaskModel(query) {
  console.log(query.data)
  return request({
    url: '/save_model',
    method: 'post',
    params: {
      project_id: query.id,
      project_model: JSON.stringify(query.data),
    },
  })
}

export function getTaskMenu() {
  return new Promise((resolve, reject) => {
    const data = [
      { label: '数据源', id: 'source', icon: 'Coin' },
      {
        label: '隐私保护算法',
        id: 'learn',
        icon: 'StarFilled',
        children: [
          { label: '标准模式', id: 'Normal Training', icon: 'Coin' },
          { label: '差分隐私', id: 'Differential Privacy Training', icon: 'Coin' },
          { label: '同态加密', id: 'Homomorphic Encryption', icon: 'Coin' },
          { label: 'XNegDL', id: 'Negative Database', icon: 'Coin' },
          { label: 'PPUTL', id: 'Improved Generative Adversarial Networks', icon: 'Coin' },
          { label: '共享权重', id: 'Weight Sharing for Collaborative Learning', icon: 'Coin' },
        ],
      },
      {
        label: '模型选择',
        id: 'learn',
        icon: 'StarFilled',
        children: [
          { label: '自定义模型', id: 'DIY', icon: 'Coin' },
          { label: 'resnet18', id: 'resnet18', icon: 'Coin' },
          { label: 'resnet50', id: 'resnet50', icon: 'Coin' },
          { label: 'densenet121', id: 'densenet121', icon: 'Coin' },
          { label: 'alexnet', id: 'alexnet', icon: 'Coin' },
          { label: 'vgg16', id: 'vgg16', icon: 'Coin' },
          { label: 'vgg19', id: 'vgg19', icon: 'Coin' },
          { label: 'inception_v3', id: 'inception_v3', icon: 'Coin' },
          { label: 'googlenet', id: 'googlenet', icon: 'Coin' },
        ],
      },
      { label: '模型对比', id: 'contrast', icon: 'Coin' },
    ]
    resolve(data)
  })
}

export function getAttackMenu() {
  return new Promise((resolve, reject) => {
    const data = [
      {
        label: '数据源',
        id: 'source',
        icon: 'Coin',
      },
      {
        label: '加密算法',
        id: 'learn',
        // icon: 'StarFilled',
        children: [
          {
            label: '标准模式',
            id: 'Normal',
            icon: 'Coin',
          },
          {
            label: '差分隐私',
            id: 'Differential Privacy Training',
            icon: 'Coin',
          },
          {
            label: '同态加密',
            id: 'Homomorphic Encryption',
            icon: 'Coin',
          },
          {
            label: '负数据库',
            id: 'Negative Database',
            icon: 'Coin',
          },
          {
            label: '改进的CGAN',
            id: '改进的CGAN',
            icon: 'Coin',
          },
          {
            label: '矩阵变换与排序',
            id: '矩阵变换与排序',
            icon: 'Coin',
          },
        ],
      },
      {
        label: '攻击算法',
        id: 'attack',
        // icon: 'StarFilled',
        children: [
          {
            label: '梯度泄露',
            id: 'Gradient Leakage',
            icon: 'Coin',
          },
          {
            label: '成员推理',
            id: 'Membership Inference',
            icon: 'Coin',
          },
          {
            label: '模型逆向',
            id: 'Model Inversion',
            icon: 'Coin',
          },
        ],
      },
      // , {
      //   label: '模型对比',
      //   id: 'contrast',
      //   icon: 'Coin'
      // }
    ]
    resolve(data)
  })
}

export function getAttackModel() {
  return new Promise((resolve, reject) => {
    const data = {
      nodes: [],
      endpoints: [],
      edges: [], // 边的对应关系(sourceId -> targetId)
      head: '',
    }
    resolve(data)
  })
}
